package com.xiaochong.tir.manager.util;

import com.xiaochong.tir.common.data.vo.SysAreaVo;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/6/11.
 * List转tree
 */
public class TreeBuilder {
    /**
     * 遍历获取父节点
     * @param treeNodes
     * @return
     */
    public static List<SysAreaVo> buildByRecursive(List<SysAreaVo> treeNodes) {
        List<SysAreaVo> trees = new ArrayList<SysAreaVo>();
        for (SysAreaVo treeNode : treeNodes) {
            if (0 == treeNode.getParent()) {
                trees.add(findChildren(treeNode,treeNodes));
            }
        }
        return trees;
    }

    /**
     * 递归存入子节点
     * @param treeNode
     * @param treeNodes
     * @return
     */
    private static SysAreaVo findChildren(SysAreaVo treeNode, List<SysAreaVo> treeNodes) {
        for (SysAreaVo it : treeNodes) {
            if(treeNode.getId().equals(it.getParent())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<SysAreaVo>());
                }
                treeNode.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return treeNode;
    }

}
